package com.cw.jeeyt.service.impl.facade;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.service.bo.facade.FacadeEnumBo;
import com.cw.jeeyt.service.converter.facade.FacadeEnumConverter;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.facade.FacadeEnumDao;
import com.cw.jeeyt.dao.model.facade.FacadeEnumDo;
import com.cw.jeeyt.dao.query.facade.FacadeEnumQuery;
import com.cw.jeeyt.service.facade.FacadeEnumService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 接口枚举接口业务实现类
 *
 * @author chenw
 * @date 2019-11-01 23:46:50
 */
@Service
public class FacadeEnumServiceImpl implements FacadeEnumService {
    @Autowired
    private FacadeEnumDao facadeEnumDao;

    /**
     * 新增接口枚举
     *
     * @param request 条件
     * @return String
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addFacadeEnum(FacadeEnumBo request) {
        if (request == null) {
            return null;
        }
        request.setCreateBy(SessionUtil.getSessionUserId());
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setCreateDate(new Date());
        request.setUpdateDate(new Date());
        facadeEnumDao.insertBySelective(FacadeEnumConverter.bo2Do(request));
        return "";
    }

    /**
     * 更新接口枚举
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifyFacadeEnum(FacadeEnumBo request) {
        if (request == null) {
            return false;
        }
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setUpdateDate(new Date());
        facadeEnumDao.updateBySelective(FacadeEnumConverter.bo2Do(request));
        return true;
    }

    /**
     * 删除接口枚举
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFacadeEnum(FacadeEnumBo request) {
        if (request == null) {
            return false;
        }
        return facadeEnumDao.deleteByKey(request.getId()) > 0;
    }

    /**
     * 分页查询接口枚举
     *
     * @param request 条件
     * @return PageInfo<FacadeEnumBo>
     */
    @Override
    public PageInfo<FacadeEnumBo> selectPage(FacadeEnumBo request, PageParam pageParam) throws Exception {
        PageInfo<FacadeEnumDo> pageInfo = facadeEnumDao.selectPage(FacadeEnumQuery.builder()
            .id(request.getId())
            .enumCode(request.getEnumCode())
            .enumName(request.getEnumName())
            .enumComment(request.getEnumComment())
            .enumItemKey(request.getEnumItemKey())
            .enumItemCode(request.getEnumItemCode())
            .enumItemDesc(request.getEnumItemDesc())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build(), pageParam);
        List<FacadeEnumBo> facadeEnums = FacadeEnumConverter.doList2BoList(pageInfo.getList());
        return new PageInfo<>(pageInfo, facadeEnums);
    }

    /**
     * 查询接口枚举详情信息
     *
     * @param request 条件
     * @return FacadeEnumBo
     */
    @Override
    public FacadeEnumBo queryFacadeEnumDetail(FacadeEnumBo request) {
        if (request == null) {
            return null;
        }
        return FacadeEnumConverter.do2Bo(facadeEnumDao.selectByKey(request.getId()));
    }

    /**
     * 根据条件查询接口枚举
     *
     * @param request 条件
     * @return List<FacadeEnumBo>
     */
    @Override
    public List<FacadeEnumBo> queryFacadeEnumList(FacadeEnumBo request) {
        if (request == null) {
            return null;
        }
        return FacadeEnumConverter.doList2BoList(facadeEnumDao.selectByExample(FacadeEnumQuery.builder()
            .id(request.getId())
            .enumCode(request.getEnumCode())
            .enumName(request.getEnumName())
            .enumComment(request.getEnumComment())
            .enumItemKey(request.getEnumItemKey())
            .enumItemCode(request.getEnumItemCode())
            .enumItemDesc(request.getEnumItemDesc())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build()));
    }

    /**
     * 根据key查询接口枚举
     *
     * @param id
     * @return
     */
    @Override
    public FacadeEnumBo selectByKey(Long id) {

        return FacadeEnumConverter.do2Bo(facadeEnumDao.selectByKey(id));
    }
}
